package com.borrowed.book.book.controller;


import com.alibaba.fastjson.JSONObject;
import com.borrowed.book.book.constant.BookConstant;
import com.borrowed.book.book.domain.*;
import com.borrowed.book.book.entity.domain.vo.ConfirmReceiptVo;
import com.borrowed.book.book.entity.domain.vo.PayOrderBaseOutVo;
import com.borrowed.book.book.factory.PayorderFatory;
import com.borrowed.book.book.service.AbstractPayOrderService;
import com.borrowed.book.system.controller.BaseController;
import com.borrowed.book.system.domain.AjaxResult;
import com.borrowed.book.system.domain.TableDataInfo;
import com.borrowed.book.weixin.service.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@Api(tags = "支付订单管理")
@RestController
@RequestMapping("/api/payOrder")
public class PayOrderController extends BaseController {

    @Qualifier("bookPayorderServiceImpl")
    @Autowired
    private AbstractPayOrderService payOrderService;
    @Autowired
    private WxPayService wxPayService;

    @ApiOperation(value = "会员订单下单前数据加载",httpMethod = "GET")
    @GetMapping("/initialCardPay")
    @ResponseBody
    @Transactional
    public AjaxResult initialCardPayInfo(InitialCarPayInDto param){
        AjaxResult result;
        try{
            payOrderService= PayorderFatory.create(BookConstant.CARD_ORDER);
            PayOrderBaseOutVo carPay=payOrderService.initialPayorderInfo(param);
            result=AjaxResult.sucess("操作成功",carPay);
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "图书租赁订单下单前数据加载",httpMethod = "POST")
    @PostMapping("/initialBookPay")
    @ResponseBody
    @Transactional
    public AjaxResult initialBookPayInfo(@RequestBody InitialBookPayInDto param){
        AjaxResult result;
        try{
            payOrderService= PayorderFatory.create(BookConstant.RENT_ORDER);
            PayOrderBaseOutVo bookPay=payOrderService.initialPayorderInfo(param);
            result=AjaxResult.sucess("操作成功",bookPay);
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "新增支付订单（金额）",httpMethod = "POST")
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult AddPayOrder(@RequestBody PayOrderInDto indto){
        AjaxResult result;
        try{
            String orderNo=null;
            payOrderService= PayorderFatory.create(indto.getOrderType());
            orderNo=payOrderService.createPayorder(indto);
            JSONObject map= wxPayService.PayTransaction(indto.getPayAmount(),orderNo);
            result=AjaxResult.sucess("操作成功",map.toString());
        }catch (Exception e){
            e.printStackTrace();
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }




    @ApiOperation(value = "支付订单一览查询",httpMethod = "GET")
    @GetMapping("/selectAdmin")
    @ResponseBody
    @Transactional
    public TableDataInfo GetPayOrder(SelectPayOrderInDto inDto){
        startPage();
        List<SelectPayOrderOutDto> data=payOrderService.selectPayOrderByOrderStatus(inDto.getOrderStatus());
        return getDataTable(data);
    }


    @ApiOperation(value = "管理端支付订单一览查询",httpMethod = "GET")
    @GetMapping("/select")
    @ResponseBody
    @Transactional
    public TableDataInfo GetPayOrderAdmin(SelectPayOrderInDto inDto){
        startPage();
        List<SelectPayOrderOutDto> data=payOrderService.selectPayOrderAdmin(inDto.getOrderStatus());
        return getDataTable(data);
    }



    @ApiOperation(value = "支付订单详细查询",httpMethod = "GET")
    @GetMapping("/detail")
    @ResponseBody
    @Transactional
    public AjaxResult GetPayOrderDetail(String orderNo) {
        AjaxResult result;
        try {
            SelectPayOrderOutDto data = payOrderService.selectPayOrderDetail(orderNo);
            result = AjaxResult.sucess("查询成功", data);
        } catch (Exception e) {
            e.printStackTrace();
            result = AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "取消支付订单",httpMethod = "PUT")
    @PutMapping("/cancel")
    @ApiImplicitParam(name="orderNo",value = "订单号",type = "String",required = true)
    @ResponseBody
    @Transactional
    public AjaxResult CanclePayOrder(String orderNo){
        AjaxResult result;
        try{
            payOrderService.cancelPayorder(orderNo);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "发货更新物流信息",httpMethod = "PUT")
    @PutMapping("/deliver")
    @ResponseBody
    @Transactional
    public AjaxResult  DeliverOrder(@RequestBody  DeliverGoodInDto param){
        AjaxResult result;
        try{
            payOrderService.deliverGood(param);
            result=AjaxResult.sucess();
        }catch (Exception e){
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value="确认收货",httpMethod = "PUT")
    @PutMapping("/confirmReceipt")
    public AjaxResult ConfirmReceipt(@RequestBody  ConfirmReceiptVo param){
        AjaxResult result;
        try{
            payOrderService.ConfirmReceipt(param);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "获取物流信息",httpMethod = "GET")
    @GetMapping("/getLogistics")
    @ResponseBody
    @Transactional
    public AjaxResult  GetLogistics(String  orderNo){
        AjaxResult result;
        try{
            String msg=payOrderService.getTransportInfo(orderNo);
            result=AjaxResult.sucess("操作成功",msg);
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }
}
